from tkinter import Tk
from tkinter import ttk
from database.table import tablee
from database.table.tablee import Employ
import tkinter as tk


def SearchEmploy():
    search_employ_screen = Tk()
    search_employ_screen.title('查询员工信息')
    screen_width, screen_height = search_employ_screen.maxsize()
    # 窗口的宽和高
    width = 605
    height = 200
    centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                              (screen_height - height) / 2)
    search_employ_screen.geometry(centre)
    search_employ_screen.resizable(0,0)

    label = tk.Label(search_employ_screen, text='请输入要查询的员工编号：', font=('楷体', 14))
    label.place(x=150, y=20)
    #编号标签文本框
    user_num_label = tk.Label(search_employ_screen, text='编号', font=('楷体', 20))
    user_num_label.place(x=150, y=60)
    user_num_text = tk.StringVar()
    user_num_entry = tk.Entry(search_employ_screen, textvariable=user_num_text,
                              width=15)
    user_num_entry.place(x=230, y=68)
    #确定按钮响应事件
    def confirm():
        id = str(user_num_entry.get())
        employee = tablee.session.query(Employ).filter(Employ.Eno == id).all()
        data = Employ.to_json(employee)
        length = len(data)

        if length == 1: #查找的员工编号存在
            search = tk.Toplevel(search_employ_screen)
            search.title(id+'员工信息')
            screen_width, screen_height = search.maxsize()
            # 窗口的宽和高
            width = 610
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            search.geometry(centre)

            tree = ttk.Treeview(search)  #创建表格对象
            tree["columns"] =("编号","姓名","密码","级别","电话","工资","备注")
            tree.column("编号", width=50)  # 设置列
            tree.column("姓名", width=50)
            tree.column("密码", width=50)
            tree.column("级别", width=50)
            tree.column("电话", width=100)
            tree.column("工资", width=50)
            tree.column("备注", width=50)
            tree.heading("编号", text="编号")  # 显示表头名
            tree.heading("姓名", text="姓名")
            tree.heading("密码", text="密码")
            tree.heading("级别", text="级别")
            tree.heading("电话", text="电话")
            tree.heading("工资", text="工资")
            tree.heading("备注", text="备注")

            for i in range(0, length):
                num = data[i]['Eno']
                name = data[i]['Ename']
                password = data[i]['Epassword']
                level = data[i]['Elevel']
                phone = data[i]['Ephone']
                pay = data[i]['Epay']
                remarks = data[i]['Eremarks']
                tree.insert("", 0, text='', value=(num, name, password, level, phone, pay, remarks))
            tree.pack()

            tablee.session.close()

        elif id == '': #员工编号为空
            search = tk.Toplevel(search_employ_screen)
            search.title("Error")
            screen_width, screen_height = search.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            search.geometry(centre)
            label = tk.Label(search, text='编号不能为空，请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                search.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(search, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)
        else: #员工编号不存在
            search = tk.Toplevel(search_employ_screen)
            search.title("Error")
            screen_width, screen_height = search.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            search.geometry(centre)
            label = tk.Label(search, text='此编号不存在,请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                search.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(search, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)

    #返回按钮响应事件
    def back():
        search_employ_screen.destroy()

    # 确定按钮
    user_confirm_button = tk.Button(search_employ_screen, text='确定', font=('楷体', 12),
                                    width=10, command=confirm)
    user_confirm_button.place(x=150, y=120)
    # 返回按钮
    user_back_button = tk.Button(search_employ_screen, text='返回', font=('楷体', 12),
                                 width=10, command=back)
    user_back_button.place(x=280, y=120)

    search_employ_screen.mainloop()

if __name__ == '__main__':
    SearchEmploy()